package com.xiaobai.ThreadReentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.ReentrantLock;
/*
ReentrantLock是可重入的
21:36:41.595 [main] DEBUG com.ThreadReentrantLock.TestReentrantLock -- main 方法进入了lock
21:36:41.599 [main] DEBUG com.ThreadReentrantLock.TestReentrantLock -- m1 方法进入了lock
21:36:41.599 [main] DEBUG com.ThreadReentrantLock.TestReentrantLock -- m2 方法进入了lock
 */
@Slf4j
public class TestReentrantLock {
    static final ReentrantLock lock = new ReentrantLock();

    public static void main(String[] args) {
        lock.lock();
        try {
            log.debug("main 方法进入了lock");
            m1();
        }finally {
            lock.unlock();

        }
    }

    private static void m1() {
        lock.lock();
        try {
            log.debug("m1 方法进入了lock");
            m2();
        }finally {
            lock.unlock();
        }
    }

    private static void m2() {
        lock.lock();
        try {
            log.debug("m2 方法进入了lock");
        }finally {
            lock.unlock();
        }
    }
}
